package com.fks.fks_service.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class MD5Util {

    public static String encrypt(String password,String salt) {
        try {
            // 获取MessageDigest实例，指定算法为MD5
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 计算MD5摘要
            password=password+salt;
            byte[] messageDigest = md.digest(password.getBytes());

            // 将byte数组转换为BigInteger表示
            BigInteger no = new BigInteger(1, messageDigest);

            // 将BigInteger转换为16进制表示
            StringBuilder hashtext = new StringBuilder(no.toString(16));
            while (hashtext.length() < 32) {
                hashtext.insert(0, "0");
            }
            
            return hashtext.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5加密失败", e);
        }
    }

    public static String getSalt(){
        Random random = new Random();
        String salt = "";
        int randomNumber = random.nextInt(4);
        char randomLetter = (char) (random.nextInt(26) + 97);
        if (randomNumber==0){//数字加数字
            salt = String.valueOf(random.nextInt(10)) + String.valueOf(random.nextInt(10));
        }else if (randomNumber==1){//数字加字母
            salt = String.valueOf(random.nextInt(10)) + String.valueOf((char) (random.nextInt(26) + 97));
        }else if (randomNumber==2){//字母加数字
            salt = String.valueOf((char) (random.nextInt(26) + 97)) + String.valueOf(random.nextInt(10));
        }else {//字母加字母
            salt = String.valueOf((char) (random.nextInt(26) + 97)) + String.valueOf((char) (random.nextInt(26) + 97));
        }
        return salt;
    }


}